﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace working
{
    public class ConfigLogic
    {
        private readonly static ConfigLogic _instence = new ConfigLogic();
        public static ConfigLogic Instence
        {
            get
            {
                return _instence;
            }
        }

        private bool initialzed;
        private const string projectsKey = "projects";
        private const string commandsKey = "commands";
        private const string btncmdKey = "buttoncmds";
        private List<CmdRoot> cmdItems;
        public List<CmdItemLeaf> btnCmds { get; private set; }
        public ProjectArray projectArray { get; private set; }
        public void Initialize()
        {
            LoadConfigProject();
            LoadCmdList();
            LoadQuickBtnCmds();
            initialzed = true;
        }
        public CmdItem[] GetCmdItems(string name)
        {
            CmdRoot root = cmdItems.Find(x => x.name == name);
            if (root == null)
            {
                root = new CmdRoot();
                root.name = name;
                root.path = "";
                root.text = name;
                cmdItems.Add(root);
            }
            return root.childItems;
        }

        public void SetCmdItems(string name, List<CmdItem1> cmdItemArray)
        {
            var root = cmdItems.Find(x => x.name == name);
            if (root != null)
            {
                root.RemoveChilds();
                root.AddChilds(cmdItemArray.ToArray());
            }
        }

      
        public CmdItemLeaf GetBtnCmd(int index)
        {
            if (btnCmds != null && btnCmds.Count > index && index >-1)
            {
                return btnCmds[index];
            }
            return null;
        }

        public CmdItemLeaf[] GetQuickBtnCmds()
        {
            return btnCmds.ToArray();
        }

        public void SaveQuickBtnCmds()
        {
            if (!initialzed) return;
            var info = Util.ToB64String(btnCmds);
            Util.SetConfigItem(btncmdKey, info);
        }

        private void LoadQuickBtnCmds()
        {
            if (Util.ExistConfigItem(btncmdKey))
            {
                var info = Util.GetConfigItem(btncmdKey);
                btnCmds = Util.FromB64String<List<CmdItemLeaf>>(info);
            }

            if (btnCmds == null)
            {
                btnCmds = new List<CmdItemLeaf>();
            }
        }

        public void AddCmdItem(string cmdName, CmdItem1 cmdItem)
        {
            var root = cmdItems.Find(x => x.name == cmdName);
            if (root != null)
            {
                root.AddChild(cmdItem);
            }
        }

        public void MoveUpCmdItem(string cmdName, CmdItem1 cmdItem)
        {
            var root = cmdItems.Find(x => x.name == cmdName);
            if (root != null)
            {
                root.MoveUpCmdItem(cmdItem);
            }
        }

        public void MoveDownCmdItem(string cmdName, CmdItem1 cmdItem)
        {
            var root = cmdItems.Find(x => x.name == cmdName);
            if (root != null)
            {
                root.MoveDownCmdItem(cmdItem);
            }
        }

        public void RemoveCmdItem(string cmdName, CmdItem1 cmdItem)
        {
            var root = cmdItems.Find(x => x.name == cmdName);
            if (root != null)
            {
                root.RemoveChild(cmdItem);
            }
        }

        private void LoadConfigProject()
        {
            if (Util.ExistConfigItem(projectsKey))
            {
                var info = Util.GetConfigItem(projectsKey);
                projectArray = Util.FromB64String<ProjectArray>(info);
            }
            if (projectArray == null)
            {
                projectArray = new ProjectArray();
            }
        }

        public void SaveConfigProject()
        {
            if (!initialzed) return;
            var info = Util.ToB64String(projectArray);
            Util.SetConfigItem(projectsKey, info);
        }

        private void LoadCmdList()
        {
            if (Util.ExistConfigItem(commandsKey))
            {
                var info = Util.GetConfigItem(commandsKey);
                cmdItems = Util.FromB64String<List<CmdRoot>>(info);
            }

            if (cmdItems == null)
            {
                cmdItems = new List<CmdRoot>();
            }
        }

        public void SaveCmdList()
        {
            if (!initialzed) return;
            var info = Util.ToB64String(cmdItems);
            Util.SetConfigItem(commandsKey, info);
        }
        public string[] GetProjectNames()
        {
            if (projectArray == null)
                return null;
            return projectArray.projectNames.ToArray();
        }
        public string[] GetProjectPaths()
        {
            if (projectArray == null)
                return null;
            return projectArray.projectPaths.ToArray();
        }

    }
}
